﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ruandao.Misc
{
    public class IdGenerator
    {
        Random rnd1;
        Random rnd2;
        static string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        public IdGenerator()
        {
            rnd1 = new Random((int)DateTime.Now.Ticks);
            rnd2 = new Random();
        }
        public string NewId()
        {
            int year = (DateTime.Today.Year - 2010) % 62;
            int day = (DateTime.Today.DayOfYear) % 62;
            return chars[year] + chars[day] + "-" + Id(rnd1.Next()) + "-" + Id(rnd2.Next());
        }
        string Id(int x)
        {
            char[] id = new char[] { 'a', 'a', 'a', 'a', 'a' };
            int i = 0;
            do
            {
                id[i++] = chars[x % 62];
                x = x / 26;
            } while (x != 0 && i != 5);

            return new string(id);
        }
    }
}
